﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MyImaginaryFriends_Redux.Screens.GamePlayScreen_Classes
{
    class Camera
    {
        public Vector2 v2Position;
        private Vector2 cameraMovement = Vector2.Zero;
        private Vector2 maxCameraPosition = Vector2.Zero;

        private const float ViewMarginX = 0.1f;
        private const float ViewMarginY = 0.15f;

        private float marginWidth;
        private float marginHeight;

        private float marginLeft;
        private float marginRight;

        private float marginTop;
        private float marginBottom;

        


        public void ScrollCamera(SpriteBatch spriteBatch, PlayerEntity player, TileMap_Colliding collisionMap)
        {
            //calculate the edges of the screen.
            marginWidth = spriteBatch.GraphicsDevice.Viewport.Width * ViewMarginX;
            marginHeight = spriteBatch.GraphicsDevice.Viewport.Height * ViewMarginY;

            marginLeft = v2Position.X + (marginWidth * 2.5f);
            marginRight = v2Position.X /*+ spriteBatch.GraphicsDevice.Viewport.Width*/ + (marginWidth * 2.5f);

            marginTop = v2Position.Y + marginHeight;
            marginBottom = v2Position.Y +/* spriteBatch.GraphicsDevice.Viewport.Height -*/ marginHeight;

            cameraMovement = Vector2.Zero;

            if (player.v2_Position.X < marginLeft)
            {
                cameraMovement.X = player.v2_Position.X - marginLeft;
            }
            else if (player.v2_Position.X > marginRight)
            {
                cameraMovement.X = player.v2_Position.X - marginRight;
            }

            if (player.v2_Position.Y < marginTop)
            {
                cameraMovement.Y = player.v2_Position.Y - marginTop;
            }
            else if (player.v2_Position.Y > marginBottom)
            {
                cameraMovement.Y = player.v2_Position.Y - marginBottom;
            }

            maxCameraPosition.X = CollidingTile.i_TileWidth * collisionMap.Tiles.GetLength(0) - spriteBatch.GraphicsDevice.Viewport.Width / 2.0f;
            maxCameraPosition.Y = CollidingTile.i_TileHeight * collisionMap.Tiles.GetLength(1) - spriteBatch.GraphicsDevice.Viewport.Height / 2.0f;

            v2Position.X = (MathHelper.Clamp(v2Position.X + cameraMovement.X, 0.0f, maxCameraPosition.X));
            v2Position.Y = (MathHelper.Clamp(v2Position.Y + cameraMovement.Y, 15.0f, maxCameraPosition.Y));
        }

        public float GetMarginTop()
        {
            return marginTop;
        }
        public float GetMarginBottom()
        {
            return marginBottom;
        }

        public float GetMarginLeft()
        {
            return marginLeft;
        }

        public float GetMarginRight()
        {
            return marginRight;
        }

        public float GetMarginWidth()
        {
            return marginWidth;
        }

        public float GetMarginHeight()
        {
            return marginHeight;
        }

        public Vector2 GetMaxCameraPosition()
        {
            return maxCameraPosition;
        }

        public Vector2 GetCameraMovement()
        {
            return cameraMovement;
        }

    }
}
